
jQuery.fn.scrollTo = function (speed) {
    var targetOffset = $(this).offset().top;
    $('html,body').stop().animate({scrollTop: targetOffset}, speed);
    return false;
};
$(window).scroll(function () {
    if ($(window).scrollTop() > 500) {
        $("[data-go-top]").show(400);
    } else {
        $("[data-go-top]").hide(400);
    }
});
$(function () {
    $("[data-go-top]").click(function () {
        $('body').scrollTo(400);
        return false;
    });
});

/*分页*/
function Page(json) {
    this.nowPage = parseInt(json.nowPage);   //当前页
    this.parent = json.parent;  //分页内容所放的div元素
    this.call = json.callback;
    this.totalCount = json.totalCount;
    this.pageSize = json.pageSize;
    this.checkPageNumber = json.checkPageNumber;
    this.totalPage = Math.ceil(this.totalCount / this.pageSize); //总页数
    this.html = "";
    this.setting = json.setting || {
            defaultPage: 5,         //默认展示的页数
            firstPageText: "首页",  //第一页的字 （可以是：Home）
            prevPageText: "上一页", //上一页的字
            nextPageText: "下一页", //下一页的字
            lastPageText: "尾页"    //尾页的字
        };
    this.centPage = Math.ceil(this.setting.defaultPage / 2);  //中间页
    this.init(); //初始化
}
//初始化函数
Page.prototype.init = function () {
    this.parent.html(""); //清空
    this.firstPage();  //首页
    this.prevPageText(); //上一页
    this.everyPage();  //分页
    this.nextPage();   //下一页
    this.lastPage();   //尾页
    this.totalPageText(); //页数显示信息
    this.parent.append(this.html);
    this.callback();
    this.goPage();
};
// 跳转
Page.prototype.goPage = function() {
    var This = this,$input = $('.checkPage-input');
    // $input.val(this.nowPage);
    $('.checkPage-btn').click(function(){
        var goPage = $input.val();
        This.call(goPage, This.totalPage);
        $input.val(goPage);
    });
};

//循环页数
Page.prototype.everyPage = function () {
    if (this.totalPage <= this.setting.defaultPage) {
        for (var i = 1; i <= this.totalPage; i++) {
            if (this.nowPage == i) {
                this.html += "<a href='#" + i + "' class='nowPage'>" + i + "</a>";
            } else {
                this.html += "<a href='#" + i + "'>" + i + "</a>";
            }
        }
    } else {
        for (var i = 1; i <= this.setting.defaultPage; i++) {
            var page1 = this.nowPage - this.centPage + i;
            var page2 = this.totalPage - this.setting.defaultPage + i;
            if (this.nowPage < this.centPage) {
                if (this.nowPage == i) {
                    this.html += "<a href='#" + i + "' class='nowPage'>" + i + "</a>";
                } else {
                    this.html += "<a href='#" + i + "'>" + i + "</a>";
                }
            } else if (this.nowPage > this.totalPage - this.centPage) {
                if (this.setting.defaultPage - (this.totalPage - this.nowPage) == i) {
                    this.html += "<a href='#" + this.nowPage + "' class='nowPage'>" + this.nowPage + "</a>";
                } else {
                    this.html += "<a href='#" + page2 + "'>" + page2 + "</a>";
                }
            } else {
                if (this.centPage == i) {
                    this.html += "<a href='#" + page1 + "' class='nowPage'>" + page1 + "</a>";
                } else {
                    this.html += "<a href='#" + page1 + "'>" + page1 + "</a>";
                }
            }
        }
    }
};
//首页
Page.prototype.firstPage = function () {
    if (this.nowPage > this.centPage && this.totalPage >= this.setting.defaultPage + 1) {
        this.html += "<a href='#1'>" + this.setting.firstPageText + "</a>";
    }
};
//上一页
Page.prototype.prevPageText = function () {
    if (this.nowPage >= 2) {
        this.html += "<a href='#" + (this.nowPage - 1) + "'>" + this.setting.prevPageText + "</a>";
    }
};
//下一页
Page.prototype.nextPage = function () {
    if (this.totalPage - this.nowPage >= 1) {
        this.html += "<a href='#" + (this.nowPage + 1) + "'>" + this.setting.nextPageText + "</a>";
    }
};
//尾页
Page.prototype.lastPage = function () {
    if (this.totalPage - this.nowPage >= this.centPage && this.totalPage > this.setting.defaultPage) {
        this.html += "<a href='#" + this.totalPage + "'>" + this.setting.lastPageText + "</a>";
    }
};
//总共页数
Page.prototype.totalPageText = function () {
    this.html += "<input type='number' min='1' class='checkPage-input'/><button type='button' class='checkPage-btn'>跳转</button> <span class='page-msg'>共<span>" + this.totalCount + "</span>条记录  每页<span>" + this.pageSize + "</span>条  第<span>" + this.nowPage + "</span>页/共<span>" + this.totalPage + "</span>页</span>";
};
//点击分页执行的函数
Page.prototype.callback = function () {
    var This = this;
    //给每个a绑定事件
    this.parent.find("a").bind("click", function () {
        // Layer.loading({width: 300, height: 110, srcType: 3, title: "正在加载中，请稍后..."});
        This.parent.html(""); //清空
        var nowPage = $(this).attr("href").substring(1);
        //写入分页
        new Page({
            parent: This.parent,
            nowPage: nowPage,
            totalPage: This.totalPage,
            pageSize: This.pageSize,
            totalCount: This.totalCount,
            setting: This.setting,
            callback: This.call
        });
        This.call(nowPage, This.totalPage);   //传参
        return false;
    });
};
//导出excel
function ExampleFile(name) {
    $('.dataTables-example').DataTable({
        dom: '<"html5buttons"B>lTfgitp',
        buttons: [
            {extend: 'excel', title: name, text: '导出excel', className: 'excelButton'},
        ],
        "paging": false,
        "searching": false,
    });
}
//  script
$(function () {
    //左边菜单
    var _href = location.href.split("/");
    $("#side-menu a").each(function () {
        $(this).parent().removeClass("active");
        var thisHref = $(this).attr("href").split("/");
        if (_href[_href.length - 1] == thisHref[thisHref.length - 1] && _href[_href.length - 2] == thisHref[thisHref.length - 2]) {
            $(this).parents("li").addClass("active");
            $(this).parents("ul.nav").removeClass("collapse");
        }
    });
    //status-nav
    $(".status-nav li").click(function () {
        $(this).addClass("active").siblings().removeClass("active");
    });
    //上传文件
    $(".file-input input").change(function () {
        var str = $(this).val();
        var arr = str.split('\\');//注split可以用字符或字符串分割
        var fileName = arr[arr.length - 1];//这就是要取得的文件名称
        $(this).nextAll("span").text(fileName);
    });
});

/**
 * 监听文本数量计数器
 * @param id
 * @param length
 */
function bindEventer(id,length) {
    $('#' + id).on('keyup', function () {
        $('#' + id).next('.help-block').find('i').html($('#' + id).val().length + "/" + length);
    });
}
/**
 * 监听文本数量计数器
 * @param classId
 * @param length
 */
function bindClassEventer(classId, length) {
    $('.' + classId).prev().on('keyup', function () {
        $('.' + classId).html($('.' + classId).prev().val().length + "/" + length);
    });
}
